function getCategory() {
    request({
        type: 'get',
        url: 'http://localhost:8080/api/v1/admin/category/list',
        success: function (res) {
            if (res.code === 200) {
                console.log(res);
                const htmlStr = template('categoryTemplate', res)
                $('tbody').html(htmlStr)
            }
        }
    })
}
getCategory()

//点击删除
function deleteCategory(id) {
    request({
        type: 'post',
        url: 'http://localhost:8080/api/v1/admin/category/delete',
        data: { id },
        success: (res) => {
            if (res.code === 204) {
                alert('删除成功')
                getCategory()
            }
        }
    })
}
//点击新增
function showCategory() {
    $('.add_category_wraper').show()
}

//添加分类
function addCategory() {
    // const id = this.id
    const name = $('#name').val();
    const slug = $('#slug').val();
    if (this.id) {
        request({
            type: 'post',
            url: 'http://localhost:8080/api/v1/admin/category/edit',
            data: { id, name, slug },
            success: (res) => {
                hideCategory()
                getCategory()
            }
        })
    } else {
        request({
            type: 'post',
            url: 'http://localhost:8080/api/v1/admin/category/add',
            data: { name, slug },
            success: (res) => {
                console.log(res);
                hideCategory()
                getCategory()
            }
        })
    }
}
//隐藏新增分类
function hideCategory() {
    $('.add_category_wraper').hide()
    //每次隐藏的时候数据清空
    $('#slug').val('')
    $('#name').val('')
    this.id = null
}
//编辑分类功能
//编辑的时候数据回显
function editCategory(data) {
    showCategory()
    $('#name').val(data.name)
    $('#slug').val(data.slug)
    this.id = data.id
}